<?php
namespace Admin\Model;

use Admin\AppConst\WebConst;
use Think\Model;

//用户model
class MenuModel extends BaseModel
{
    /**
     * @var MenuModel
     */
    private     static $instance;
    protected   $tableName  = 'menu';
    private     $model;
    private     $where;


    //定义所有的表字段
    private $id         = 'id';
    private $pid        = 'pid';
    private $menuName   = 'name';
    private $iconName   = 'icon_name';
    private $sort       = 'sort';
    private $url        = 'url';
    private $addtime    = 'addtime';
    private $isShow     = 'is_show';

    /**
     * Get the instance of MenuModel.
     * @return MenuModel
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new MenuModel();
        }
        return self::$instance;
    }

    public function __construct(){
        parent::__construct();
        $this->where    = $this->setDefaultWhere();
        $this->model    = M($this->tableName);
    }

    /* @function    getList   //获取菜单数据
     * @param       $isShow     bool     是否显示
     * @date        2018-07-16
     * @author      zhangle
     * @return      int
     */

    public function getList($isShow = WebConst::DEFAULT_TYPE_FALSE){
        $where  = WebConst::DEFAULT_TYPE_EMPTY_ARRAY;
        if($isShow == WebConst::DEFAULT_TYPE_TRUE){
            $where  = array(
                $this->isShow   => WebConst::MENU_SHOW_TRUE,
            );
        }

        return $this->model->where($where)->order($this->sort.' desc')->select();
    }

    /**
     * @function    createMenu  创建
     * @param       $pid        int     分类父级id
     * @param       $iconName   string  bootstrap 提供的 icon
     * @param       $name       string  菜单名称
     * @param       $sort       int     排序
     * @param       $url        string  跳转url
     * @param       $isShow      int     是否显示
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function createMenu($pid, $iconName, $name, $sort, $url, $isShow){
        $save   = array(
            $this->pid      => $pid,
            $this->iconName => $iconName,
            $this->menuName => $name,
            $this->sort     => $sort,
            $this->url      => $url,
            $this->addtime  => time(),
            $this->isShow   => $isShow,
        );
        return $this->model->add($save);
    }

    /**
     * @function    getMenuById         查询单个
     * @param       $id         int     id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function getMenuById($id){
        $where  = array(
            $this->id   => $id,
        );

        return $this->model->where($where)->find();
    }

    /**
     * @function    getListByPid 根据pid查询数据
     * @param       $pid         int     pid
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function getListByPid($pid){
        $where  = array(
            $this->pid   => $pid,
        );
        return $this->model->where($where)->select();
    }

    /**
     * @function    editMenu    更新
     * @param       $id         int     id
     * @param       $pid        int     分类父级id
     * @param       $iconName   string  bootstrap 提供的 icon
     * @param       $name       string  菜单名称
     * @param       $sort       int     排序
     * @param       $url        string  跳转url
     * @param       $isShow     int     是否显示
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function editMenu($id, $pid, $iconName, $name, $sort, $url, $isShow){
        $where  = array(
            $this->id   => $id,
        );
        $save   = array(
            $this->pid      => $pid,
            $this->iconName => $iconName,
            $this->menuName => $name,
            $this->sort     => $sort,
            $this->url      => $url,
            $this->addtime  => time(),
            $this->isShow   => $isShow,
        );
        return $this->model->where($where)->save($save);
    }

    /**
     * @function    deleteMenu  查询单个
     * @param       $id         int     id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function deleteMenu($id){
        $where  = array(
            $this->id   => $id,
        );
        return $this->model->where($where)->delete();
    }

    /**
     * @function    deleteMenu  查询单个
     * @param       $url        string  url
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function getMenuIdByUrl($url){
        $where  = array(
            $this->url   => $url,
        );
        return $this->model->where($where)->field($this->id)->find();
    }
}